import redis

with open("../config.py", "r", encoding="utf-8") as f:
    f_content = f.read()
    exec(f_content)

redisinfo2 = redisinfo2
redisinfo3 = redisinfo3
redisinfo4 = redisinfo4
redisinfo5 = redisinfo5
redisinfo11 = redisinfo11
redisinfo12 = redisinfo12
redisinfo15 = redisinfo15


class Redis:
    def __init__(self):
        self.redis2 = self.get_redis_decode(redisinfo2)
        self.redis3 = self.get_redis_decode(redisinfo3)
        self.redis4 = self.get_redis_decode(redisinfo4)
        self.redis5 = self.get_redis_decode(redisinfo5)
        self.redis11 = self.get_redis_decode(redisinfo11)
        self.redis12 = self.get_redis_decode(redisinfo12)
        self.redis15 = self.get_redis_decode(redisinfo15)

    @staticmethod
    def get_redis_decode(redisinfo):
        redis_pool = redis.ConnectionPool(host=redisinfo['ip'], port=redisinfo['port'], password=redisinfo['password'],
                                          db=redisinfo['db'], decode_responses=True)
        # redis_pool = redis.ConnectionPool(host="192.168.1.43", port=61001, password="12345678",
        #                                   db=redisinfo['db'], decode_responses=True)
        return redis.Redis(connection_pool=redis_pool, decode_responses=True)

    # db-----2
    def rdb_acvs_acvs(self, acvs_id, acvs_p_id):
        acvs_list = self.redis2.zrange(acvs_id, 0, -1)
        acvs_list1 = acvs_list
        if acvs_p_id not in acvs_list:
            acvs_list.append(acvs_p_id)
        for acvs in acvs_list:
            self.redis2.zrem(acvs, acvs_id)
            self.redis2.zrem(acvs_id, acvs)
        return acvs_list1

    # db-----3 集合 建档图片
    def rdb_get_face_id(self, face_id, acvs_id):
        return self.redis3.sismember(face_id, acvs_id)

    # db-----4
    def rdb_del_face_acvs(self, face_id, acvs_id):
        self.redis4.zrem(face_id, acvs_id)

    def rdb_get_face_acvs(self, face_id, acvs_id):
        return self.redis4.zrank(face_id, acvs_id) != None

    def rdb_zadd_face_acvs(self, face_id, acvs_id, score):
        self.redis4.zadd(face_id, {acvs_id: score})

    # db-----5
    def rdb_del_acvs_face(self, face_id, acvs_id):
        self.redis5.zrem(acvs_id, face_id)

    def reb_zadd_acvs_face(self, acvs_id, face_id, score):
        self.redis5.zadd(acvs_id, {face_id: score})

    # db-----11  del - acvs set
    def rdb_demolition_acvs(self, acvs, acvs_p_id):
        self.redis11.sadd(acvs, acvs_p_id)
        self.redis11.sadd(acvs_p_id, acvs)

    def rdb_mer_acvs(self, acvs, acvs_p_id):
        del_list = list(self.redis11.smembers(acvs))
        if acvs_p_id in del_list:
            self.redis11.srem(acvs, acvs_p_id)
            self.redis11.srem(acvs_p_id, acvs)

    # db-----12
    def rdb_demolition_face(self, acvs_id, face_id):
        self.redis12.sadd(face_id, acvs_id)

    def rdb_mer_face(self, face_id, acvs_id):
        del_list = list(self.redis12.smembers(face_id))
        if acvs_id in del_list:
            self.redis12.srem(face_id, acvs_id)
        pass

    # db-----15
    def get_face_img(self, face_id):
        return self.redis15.hgetall(face_id)

# client = Redis()
# print(client.rdb_get_face_acvs("100", "bbf3aed3-a0c9-11ea-9ec9-01288cb82d5b"))
# print(client.redis15.hgetall("10"))
